#!/bin/bash
#chkconfig:2345 99 99
#nginx service management
nginx_home=/usr/local/nginx
nginx_sbin=/$nginx_home/sbin/nginx
nginx_conf=/$nginx_home/conf/nginx.conf
nginx_pid=/$nginx_home/logs/nginx.pid
nginx_name="nginx"
. /etc/rc.d/init.d/functions
if [ ! -f $nginx_sbin ]
then
    echo "$nginx_name startup: $nginx_sbin not exists!"
    exit
fi
ss -tnlp|grep http&>/dev/null
if [ $? -eq 0 ]
then
    echo "The port is occupied"
fi
start() {
    $nginx_sbin -c $nginx_conf
    if [ $? -eq 0 ]
    then
	action $"Starting $nginx_name: " /bin/true
    else
	action $"Starting $nginx_name: " /bin/false
    fi
}
stop() {
    kill `cat $nginx_pid`
    if [ $? -eq 0 ]
    then
	action $"Stopping $nginx_name: " /bin/true
    else
	action $"Stopping $nginx_name: " /bin/false
    fi
}
restart() {
    stop
    start
}
check() {
    $nginx_sbin -c $nginx_conf -t
}
reload() {
    kill -HUP `cat $nginx_pid` && echo "reload successful"
}
reopen() {
    kill -USR1 `cat $nginx_pid` && echo "reopen successful"
}
case "$1" in
    start)start;;
    stop)stop;;
    restart)restart;;
    check)check;;
    status)status -p $nginx_pid;;
    reload)reload;;
    reopen)reopen;;
    *)echo $"Use parameter has: $0 {start|stop|restart|reload|status|check|reopen}"
    echo "start ----This is the startup nginx service"
    echo "stop ----This is the out of nginx service"
    echo "restart ----This is the restart nginx service"
    echo "reload ----This is the overloaded nginx service"
    echo "status ----This is the viewing nginx service"
    echo "check ----This is the inspection service"
    echo "reopen ----This is the reopening service"
    exit 1
esac